package com.itheima.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.PageVo;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.GrantRoleDto;
import com.itheima.admin.dto.UserDto;
import com.itheima.admin.mapper.DepartmentMapper;
import com.itheima.admin.mapper.UserMapper;
import com.itheima.admin.mapper.UserRoleMapper;
import com.itheima.admin.pojo.Department;
import com.itheima.admin.pojo.User;
import com.itheima.admin.service.IUserService;
import com.itheima.admin.vo.GrantRoleVo;
import com.itheima.admin.vo.UserPageVo;
import com.itheima.admin.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author WXL
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public UserVo queryById(String id) {
        User user = userMapper.selectById(id);
        UserVo userVo = new UserVo();

        BeanUtils.copyProperties(user, userVo);

        return userVo;
    }

    @Override
    public boolean deleteById(String id) {

        userRoleMapper.deleteByUserId(id);

        return userMapper.delleteById(id);
    }

    @Override
    public boolean addUser(UserDto userDto) {
        User user = new User();

        Department department = departmentMapper.queryById(userDto.getDeptId());

        BeanUtils.copyProperties(userDto, user);
        user.setDeptName(department.getDeptName());
        user.setUserName(userDto.getUserName());

        user.setUserId(UUID.randomUUID().toString());

        return userMapper.addUser(user);
    }

    @Override
    public PageVo<UserPageVo> queryByPage(PageDto pageDto) {

        //开启分页
        PageHelper.startPage(pageDto.getCurrentPage(), pageDto.getPageSize());

        List<User> pages = userMapper.selectByPage(pageDto.getStatus(),pageDto.getUsername());
        PageInfo<User> page = new PageInfo<>(pages);

        //List<User>  转成  List<UserPageVo>  流式编程
        List<UserPageVo> list = page.getList().stream().map(
                user -> {
                    UserPageVo userPageVo = new UserPageVo();
                    BeanUtils.copyProperties(user,userPageVo);
                    userPageVo.setStatus(user.getState());
                    return userPageVo;
                }
        ).collect(Collectors.toList());

        return new PageVo<UserPageVo>(list,Long.valueOf(page.getTotal()).intValue());
    }

    @Override
    public boolean editUser(UserVo userVo) {
        User user = new User();

        BeanUtils.copyProperties(userVo, user);

        return userMapper.updateUser(user);
    }

    @Override
    public boolean grantRole(GrantRoleDto grantRoleDto) {
        List<GrantRoleVo> list = new ArrayList<>();
        for (int i=0;i<grantRoleDto.getRoleIds().size(); i++){
            list.add(new GrantRoleVo(grantRoleDto.getUserId(),grantRoleDto.getRoleIds().get(i)));
        }
        System.out.println(list);
        return userMapper.grantRole(list);
    }
}
